<template>
  <div class="page-container">
    <!-- 顶部导航 -->
    <header class="header">
      <div class="header-content">
        <div class="header-flex">
          <div>
            <h1 class="header-title">防返贫检测系统工作台</h1>
            <p class="header-subtitle">{{ user.department }} · {{ user.position }}</p>
          </div>
          <div class="header-user">
            <p class="user-welcome">欢迎，{{ user.name }}</p>
            <p class="current-time">{{ formatTime(currentTime) }}</p>
          </div>
        </div>
      </div>
    </header>

    <div class="main-content">
      <!-- 统计卡片 -->
      <div class="stats-grid">
        <div v-for="(stat, index) in stats" :key="index" class="stat-card">
          <div class="stat-card-content">
            <div :class="`stat-icon ${stat.bgColor} ${stat.color}`">
              <component :is="stat.icon" class="icon-size" />
            </div>
            <div class="stat-info">
              <p class="stat-title">{{ stat.title }}</p>
              <p class="stat-count">{{ stat.count }}</p>
            </div>
          </div>
        </div>
      </div>

      <div class="content-grid">
        <!-- 待处理消息 -->
        <div class="content-card">
          <div class="card-header">
            <div class="card-header-flex">
              <h2 class="card-title">
                <Bell class="card-icon blue" />
                待处理消息
              </h2>
              <span class="badge urgent"> {{ urgentMessagesCount }} 紧急 </span>
            </div>
          </div>
          <div class="card-list">
            <div
              v-for="message in pendingMessages"
              :key="message.id"
              class="list-item"
              @click="handleMessageClick(message)"
            >
              <div class="list-item-content">
                <div class="list-item-main">
                  <div class="item-tags">
                    <span :class="`tag ${message.urgent ? 'tag-urgent' : 'tag-normal'}`">
                      {{ getTypeText(message.type) }}
                    </span>
                    <span v-if="message.urgent" class="urgent-label">紧急</span>
                  </div>
                  <h3 class="item-title">{{ message.title }}</h3>
                  <p class="item-content">{{ message.content }}</p>
                  <p class="item-time">{{ message.createTime }}</p>
                </div>
                <ChevronRight class="chevron-icon" />
              </div>
            </div>
          </div>
          <div class="card-footer">
            <button class="view-all-btn" @click="viewAllMessages">查看全部消息 →</button>
          </div>
        </div>

        <!-- 未完成工作 -->
        <div class="content-card">
          <div class="card-header">
            <h2 class="card-title">
              <Clock class="card-icon orange" />
              未完成工作
            </h2>
          </div>
          <div class="card-list">
            <div
              v-for="work in unfinishedWork"
              :key="work.id"
              class="list-item"
              @click="handleWorkClick(work)"
            >
              <div class="list-item-content">
                <div class="list-item-main">
                  <span class="tag tag-warning">
                    {{ getTypeText(work.type) }}
                  </span>
                  <h3 class="item-title">{{ work.title }}</h3>
                  <p class="item-content">农户：{{ work.farmer }}</p>
                  <div class="progress-section">
                    <div class="progress-info">
                      <span>进度：{{ work.progress }}%</span>
                      <span>截止：{{ work.deadline }}</span>
                    </div>
                    <div class="progress-bar">
                      <div
                        :class="`progress-fill ${getProgressColor(work.progress)}`"
                        :style="{ width: `${work.progress}%` }"
                      ></div>
                    </div>
                  </div>
                </div>
                <ChevronRight class="chevron-icon" />
              </div>
            </div>
          </div>
          <div class="card-footer">
            <button class="view-all-btn" @click="viewAllWork">查看全部工作 →</button>
          </div>
        </div>

        <!-- 上级通知 -->
        <div class="content-card">
          <div class="card-header">
            <div class="card-header-flex">
              <h2 class="card-title">
                <FileText class="card-icon green" />
                上级通知
              </h2>
              <span class="badge normal"> {{ unreadNotificationsCount }} 未读 </span>
            </div>
          </div>
          <div class="card-list">
            <div
              v-for="notification in notifications"
              :key="notification.id"
              class="list-item"
              @click="handleNotificationClick(notification)"
            >
              <div class="list-item-content">
                <div class="list-item-main">
                  <div class="item-tags">
                    <span v-if="notification.important" class="tag tag-urgent"> 重要 </span>
                    <span v-if="!notification.read" class="unread-dot"></span>
                  </div>
                  <h3 :class="`item-title ${notification.read ? 'read' : 'unread'}`">
                    {{ notification.title }}
                  </h3>
                  <p class="item-content notification-content">
                    {{ notification.content }}
                  </p>
                  <div class="notification-meta">
                    <span>{{ notification.sender }}</span>
                    <span>{{ notification.sendTime }}</span>
                  </div>
                </div>
                <ChevronRight class="chevron-icon" />
              </div>
            </div>
          </div>
          <div class="card-footer">
            <button class="view-all-btn" @click="viewAllNotifications">查看全部通知 →</button>
          </div>
        </div>
      </div>

      <!-- 快速操作 -->
      <div class="quick-actions">
        <h2 class="quick-actions-title">快速操作</h2>
        <div class="quick-actions-grid">
          <button
            v-for="(action, index) in quickActions"
            :key="index"
            :class="`quick-action-btn ${action.color}`"
            @click="handleQuickAction(action.name)"
          >
            {{ action.name }}
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted, computed } from 'vue'
import {
  Bell,
  FileText,
  Users,
  AlertTriangle,
  CheckCircle,
  Clock,
  ChevronRight,
} from 'lucide-vue-next'

// 数据类型定义
interface User {
  name: string
  department: string
  position: string
}

interface StatCard {
  title: string
  count: number
  icon: any
  color: string
  bgColor: string
}

interface PendingMessage {
  id: string
  type: 'clue' | 'approval' | 'visit' | 'report'
  title: string
  content: string
  createTime: string
  urgent: boolean
}

interface UnfinishedWork {
  id: string
  type: 'plan' | 'record' | 'application' | 'verification'
  title: string
  farmer: string
  deadline: string
  progress: number
}

interface Notification {
  id: string
  title: string
  content: string
  sender: string
  sendTime: string
  read: boolean
  important: boolean
}

interface QuickAction {
  name: string
  color: string
}

// 响应式数据
const currentTime = ref(new Date())
let timer: number | null = null

const user = ref<User>({
  name: '张三',
  department: '平山县扶贫办',
  position: '帮扶干部',
})

// 统计数据
const stats = ref<StatCard[]>([
  {
    title: '待核实线索',
    count: 8,
    icon: AlertTriangle,
    color: 'text-orange-600',
    bgColor: 'bg-orange-50',
  },
  {
    title: '待审批申请',
    count: 3,
    icon: FileText,
    color: 'text-blue-600',
    bgColor: 'bg-blue-50',
  },
  {
    title: '计划走访农户',
    count: 12,
    icon: Users,
    color: 'text-green-600',
    bgColor: 'bg-green-50',
  },
  {
    title: '已完成任务',
    count: 25,
    icon: CheckCircle,
    color: 'text-gray-600',
    bgColor: 'bg-gray-50',
  },
])

// 待处理消息
const pendingMessages = ref<PendingMessage[]>([
  {
    id: '1',
    type: 'clue',
    title: '新的风险线索需要核实',
    content: '李四家庭收入突然减少，需要实地核实情况',
    createTime: '2025-06-12 09:30',
    urgent: true,
  },
  {
    id: '2',
    type: 'approval',
    title: '高风险标记申请待审批',
    content: '王五申请将张六标记为高风险户',
    createTime: '2025-06-12 08:45',
    urgent: false,
  },
  {
    id: '3',
    type: 'visit',
    title: '本周走访计划提醒',
    content: '本周计划走访5户农户，请及时安排',
    createTime: '2025-06-11 16:20',
    urgent: false,
  },
])

// 未完成工作
const unfinishedWork = ref<UnfinishedWork[]>([
  {
    id: '1',
    type: 'plan',
    title: '帮扶计划制定',
    farmer: '李明',
    deadline: '2025-06-15',
    progress: 60,
  },
  {
    id: '2',
    type: 'record',
    title: '帮扶记录填报',
    farmer: '王强',
    deadline: '2025-06-13',
    progress: 30,
  },
  {
    id: '3',
    type: 'verification',
    title: '线索核实报告',
    farmer: '陈华',
    deadline: '2025-06-14',
    progress: 80,
  },
])

// 上级通知
const notifications = ref<Notification[]>([
  {
    id: '1',
    title: '关于开展防返贫监测专项检查的通知',
    content: '各单位要认真组织开展防返贫监测专项检查工作，确保不漏一户、不落一人...',
    sender: '市扶贫办',
    sendTime: '2025-06-11 14:30',
    read: false,
    important: true,
  },
  {
    id: '2',
    title: '6月份帮扶工作月报提交要求',
    content: '请各帮扶责任人于6月30日前提交本月帮扶工作总结报告...',
    sender: '县扶贫办',
    sendTime: '2025-06-10 10:15',
    read: true,
    important: false,
  },
  {
    id: '3',
    title: '防返贫监测系统升级维护通知',
    content: '系统将于6月15日晚22:00-6月16日凌晨2:00进行升级维护...',
    sender: '系统管理员',
    sendTime: '2025-06-09 16:45',
    read: true,
    important: false,
  },
])

// 快速操作
const quickActions = ref<QuickAction[]>([
  { name: '录入线索', color: 'blue-500-hover' },
  { name: '农户档案', color: 'green-500-hover' },
  { name: '制定计划', color: 'purple-500-hover' },
  { name: '填报记录', color: 'orange-500-hover' },
  { name: '走访管理', color: 'red-500-hover' },
  { name: '数据分析', color: 'indigo-500-hover' },
])

// 计算属性
const urgentMessagesCount = computed(() => pendingMessages.value.filter((m) => m.urgent).length)

const unreadNotificationsCount = computed(() => notifications.value.filter((n) => !n.read).length)

// 方法
const formatTime = (date: Date): string => {
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
  })
}

const getTypeText = (type: string): string => {
  const typeMap: Record<string, string> = {
    clue: '线索核实',
    approval: '申请审批',
    visit: '走访提醒',
    report: '报告提交',
    plan: '帮扶计划',
    record: '帮扶记录',
    application: '申请处理',
    verification: '核实报告',
  }
  return typeMap[type] || type
}

const getProgressColor = (progress: number): string => {
  if (progress >= 80) return 'bg-green-500'
  if (progress >= 50) return 'bg-yellow-500'
  return 'bg-red-500'
}

// 事件处理函数
const handleMessageClick = (message: PendingMessage) => {
  console.log('点击消息:', message)
  // 这里可以跳转到具体的处理页面
}

const handleWorkClick = (work: UnfinishedWork) => {
  console.log('点击工作:', work)
  // 这里可以跳转到具体的工作页面
}

const handleNotificationClick = (notification: Notification) => {
  console.log('点击通知:', notification)
  // 标记为已读并跳转
  notification.read = true
}

const handleQuickAction = (actionName: string) => {
  console.log('快速操作:', actionName)
  // 这里可以跳转到对应的功能页面
}

const viewAllMessages = () => {
  console.log('查看全部消息')
  // 跳转到消息列表页面
}

const viewAllWork = () => {
  console.log('查看全部工作')
  // 跳转到工作列表页面
}

const viewAllNotifications = () => {
  console.log('查看全部通知')
  // 跳转到通知列表页面
}

// 生命周期
onMounted(() => {
  timer = setInterval(() => {
    currentTime.value = new Date()
  }, 1000)
})

onUnmounted(() => {
  if (timer) {
    clearInterval(timer)
  }
})
</script>
<style scoped>
/* 页面容器 */
.page-container {
  min-height: 100vh;
  background-color: #f9fafb;
}

/* 顶部导航 */
.header {
  background-color: white;
  box-shadow: 0 1px 2px 0 rgb(0 0 0 / 0.05);
  border-bottom: 1px solid #e5e7eb;
  position: sticky;
  top: 0;
  z-index: 10;
}

.header-content {
  max-width: 80rem;
  margin-left: auto;
  margin-right: auto;
  padding-left: 1.5rem;
  padding-right: 1.5rem;
  padding-top: 1rem;
  padding-bottom: 1rem;
}

.header-flex {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-title {
  font-size: 1.5rem;
  line-height: 2rem;
  font-weight: 700;
  color: #111827;
  margin: 0;
}

.header-subtitle {
  font-size: 0.875rem;
  line-height: 1.25rem;
  color: #4b5563;
  margin-top: 0.25rem;
  margin-bottom: 0;
}

.header-user {
  text-align: right;
}

.user-welcome {
  font-size: 1.125rem;
  line-height: 1.75rem;
  font-weight: 500;
  color: #111827;
  margin: 0;
}

.current-time {
  font-size: 0.875rem;
  line-height: 1.25rem;
  color: #6b7280;
  margin: 0;
}

/* 主要内容区域 */
.main-content {
  max-width: 80rem;
  margin-left: auto;
  margin-right: auto;
  padding-left: 1.5rem;
  padding-right: 1.5rem;
  padding-top: 1.5rem;
  padding-bottom: 1.5rem;
}

/* 统计卡片网格 */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(1, minmax(0, 1fr));
  gap: 1.5rem;
  margin-bottom: 2rem;
}

@media (min-width: 768px) {
  .stats-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (min-width: 1024px) {
  .stats-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}

.stat-card {
  background-color: white;
  border-radius: 0.5rem;
  box-shadow: 0 1px 2px 0 rgb(0 0 0 / 0.05);
  padding: 1.5rem;
  border: 1px solid #e5e7eb;
  transition: all 0.3s ease;
}

.stat-card:hover {
  box-shadow: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -2px rgb(0 0 0 / 0.05);
  transform: translateY(-2px);
}

.stat-card-content {
  display: flex;
  align-items: center;
}

.stat-icon {
  padding: 0.75rem;
  border-radius: 0.5rem;
}

.stat-icon.bg-orange-50 {
  background-color: #fff7ed;
}

.stat-icon.text-orange-600 {
  color: #ea580c;
}

.stat-icon.bg-blue-50 {
  background-color: #eff6ff;
}

.stat-icon.text-blue-600 {
  color: #2563eb;
}

.stat-icon.bg-green-50 {
  background-color: #f0fdf4;
}

.stat-icon.text-green-600 {
  color: #16a34a;
}

.stat-icon.bg-gray-50 {
  background-color: #f9fafb;
}

.stat-icon.text-gray-600 {
  color: #4b5563;
}

.icon-size {
  width: 1.5rem;
  height: 1.5rem;
}

.stat-info {
  margin-left: 1rem;
}

.stat-title {
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 500;
  color: #4b5563;
  margin: 0;
}

.stat-count {
  font-size: 1.5rem;
  line-height: 2rem;
  font-weight: 700;
  color: #111827;
  margin: 0;
}

/* 内容网格 */
.content-grid {
  display: grid;
  grid-template-columns: repeat(1, minmax(0, 1fr));
  gap: 1.5rem;
}

@media (min-width: 1024px) {
  .content-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

/* 内容卡片 */
.content-card {
  background-color: white;
  border-radius: 0.5rem;
  box-shadow: 0 1px 2px 0 rgb(0 0 0 / 0.05);
  border: 1px solid #e5e7eb;
}

.card-header {
  padding: 1.5rem;
  border-bottom: 1px solid #e5e7eb;
}

.card-header-flex {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.card-title {
  font-size: 1.125rem;
  line-height: 1.75rem;
  font-weight: 600;
  color: #111827;
  display: flex;
  align-items: center;
  margin: 0;
}

.card-icon {
  width: 1.25rem;
  height: 1.25rem;
  margin-right: 0.5rem;
}

.card-icon.blue {
  color: #2563eb;
}

.card-icon.orange {
  color: #ea580c;
}

.card-icon.green {
  color: #16a34a;
}

/* 徽章 */
.badge {
  font-size: 0.75rem;
  line-height: 1rem;
  padding-left: 0.5rem;
  padding-right: 0.5rem;
  padding-top: 0.25rem;
  padding-bottom: 0.25rem;
  border-radius: 9999px;
  font-weight: 500;
}

.badge.urgent {
  background-color: #fee2e2;
  color: #991b1b;
}

.badge.normal {
  background-color: #dbeafe;
  color: #1e40af;
}

/* 列表 */
.card-list > * + * {
  border-top: 1px solid #e5e7eb;
}

.list-item {
  padding: 1rem;
  cursor: pointer;
  transition: background-color 0.15s ease;
}

.list-item:hover {
  background-color: #f9fafb;
}

.list-item-content {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
}

.list-item-main {
  flex: 1 1 0%;
}

.item-tags {
  display: flex;
  align-items: center;
}

.tag {
  font-size: 0.75rem;
  line-height: 1rem;
  padding-left: 0.5rem;
  padding-right: 0.5rem;
  padding-top: 0.25rem;
  padding-bottom: 0.25rem;
  border-radius: 0.25rem;
}

.tag-urgent {
  background-color: #fee2e2;
  color: #991b1b;
}

.tag-normal {
  background-color: #dbeafe;
  color: #1e40af;
}

.tag-warning {
  background-color: #fef3c7;
  color: #92400e;
}

.urgent-label {
  margin-left: 0.5rem;
  color: #ef4444;
  font-size: 0.75rem;
  line-height: 1rem;
}

.unread-dot {
  width: 0.5rem;
  height: 0.5rem;
  background-color: #3b82f6;
  border-radius: 9999px;
  margin-right: 0.5rem;
}

.item-title {
  font-weight: 500;
  color: #111827;
  margin-top: 0.5rem;
  margin-bottom: 0;
}

.item-title.read {
  color: #4b5563;
}

.item-title.unread {
  color: #111827;
}

.item-content {
  font-size: 0.875rem;
  line-height: 1.25rem;
  color: #4b5563;
  margin-top: 0.25rem;
  margin-bottom: 0;
}

.notification-content {
  color: #6b7280;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.item-time {
  font-size: 0.75rem;
  line-height: 1rem;
  color: #9ca3af;
  margin-top: 0.5rem;
  margin-bottom: 0;
}

.notification-meta {
  display: flex;
  justify-content: space-between;
  font-size: 0.75rem;
  line-height: 1rem;
  color: #9ca3af;
  margin-top: 0.5rem;
}

/* 进度条 */
.progress-section {
  margin-top: 0.5rem;
}

.progress-info {
  display: flex;
  justify-content: space-between;
  font-size: 0.75rem;
  line-height: 1rem;
  color: #6b7280;
  margin-bottom: 0.25rem;
}

.progress-bar {
  width: 100%;
  background-color: #e5e7eb;
  border-radius: 9999px;
  height: 0.5rem;
  overflow: hidden;
}

.progress-fill {
  height: 0.5rem;
  border-radius: 9999px;
  transition: width 0.3s ease;
}

.progress-fill.bg-green-500 {
  background-color: #22c55e;
}

.progress-fill.bg-yellow-500 {
  background-color: #eab308;
}

.progress-fill.bg-red-500 {
  background-color: #ef4444;
}

.chevron-icon {
  width: 1rem;
  height: 1rem;
  color: #9ca3af;
  margin-left: 0.5rem;
  flex-shrink: 0;
}

/* 卡片底部 */
.card-footer {
  padding: 1rem;
  border-top: 1px solid #e5e7eb;
  background-color: #f9fafb;
}

.view-all-btn {
  color: #2563eb;
  font-size: 0.875rem;
  line-height: 1.25rem;
  background: none;
  border: none;
  cursor: pointer;
  transition: color 0.15s ease;
}

.view-all-btn:hover {
  color: #1e40af;
}

/* 快速操作 */
.quick-actions {
  margin-top: 2rem;
  background-color: white;
  border-radius: 0.5rem;
  box-shadow: 0 1px 2px 0 rgb(0 0 0 / 0.05);
  border: 1px solid #e5e7eb;
  padding: 1.5rem;
}

.quick-actions-title {
  font-size: 1.125rem;
  line-height: 1.75rem;
  font-weight: 600;
  color: #111827;
  margin-bottom: 1rem;
  margin-top: 0;
}

.quick-actions-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 1rem;
}

@media (min-width: 768px) {
  .quick-actions-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}

@media (min-width: 1024px) {
  .quick-actions-grid {
    grid-template-columns: repeat(6, minmax(0, 1fr));
  }
}

/* 快速操作按钮 */
.quick-action-btn {
  color: white;
  padding: 0.75rem 1rem;
  border-radius: 0.5rem;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 500;
  border: none;
  cursor: pointer;
  transition: all 0.2s ease;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.quick-action-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.quick-action-btn.blue-500-hover {
  background-color: #3b82f6;
}

.quick-action-btn.blue-500-hover:hover {
  background-color: #2563eb;
}

.quick-action-btn.green-500-hover {
  background-color: #22c55e;
}

.quick-action-btn.green-500-hover:hover {
  background-color: #16a34a;
}

.quick-action-btn.purple-500-hover {
  background-color: #a855f7;
}

.quick-action-btn.purple-500-hover:hover {
  background-color: #9333ea;
}

.quick-action-btn.orange-500-hover {
  background-color: #f97316;
}

.quick-action-btn.orange-500-hover:hover {
  background-color: #ea580c;
}

.quick-action-btn.red-500-hover {
  background-color: #ef4444;
}

.quick-action-btn.red-500-hover:hover {
  background-color: #dc2626;
}

.quick-action-btn.indigo-500-hover {
  background-color: #6366f1;
}

.quick-action-btn.indigo-500-hover:hover {
  background-color: #4f46e5;
}
</style>